package com.material.web.controller.factory;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.material.common.core.domain.entity.SysRole;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.utils.SecurityUtils;
import com.material.common.utils.StringUtils;
import com.material.factory.vo.FactoryInfoVo;
import com.material.system.service.ISysRoleService;
import com.material.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.material.common.annotation.Log;
import com.material.common.core.controller.BaseController;
import com.material.common.core.domain.AjaxResult;
import com.material.common.enums.BusinessType;
import com.material.factory.domain.FactoryInfo;
import com.material.factory.service.IFactoryInfoService;
import com.material.common.utils.poi.ExcelUtil;
import com.material.common.core.page.TableDataInfo;

/**
 * 工厂信息Controller
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/factory/info")
public class FactoryInfoController extends BaseController
{
    @Autowired
    private IFactoryInfoService factoryInfoService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询工厂信息列表
     */
    @PreAuthorize("@ss.hasPermi('factory:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(FactoryInfo factoryInfo)
    {
        startPage();
        List<FactoryInfoVo> list = factoryInfoService.selectFactoryInfoList(factoryInfo);
        return getDataTable(list);
    }


    /**
     * 获取工厂信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('factory:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(factoryInfoService.selectFactoryInfoById(id));
    }

    /**
     * 新增工厂信息
     */
    @PreAuthorize("@ss.hasPermi('factory:info:add')")
    @Log(title = "工厂信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FactoryInfo factoryInfo)
    {
        return toAjax(factoryInfoService.insertFactoryInfo(factoryInfo));
    }

    /**
     * 修改工厂信息
     */
    @PreAuthorize("@ss.hasPermi('factory:info:edit')")
    @Log(title = "工厂信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FactoryInfo factoryInfo)
    {
        return toAjax(factoryInfoService.updateFactoryInfo(factoryInfo));
    }

    /**
     * 删除工厂信息
     */
    @PreAuthorize("@ss.hasPermi('factory:info:remove')")
    @Log(title = "工厂信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(factoryInfoService.deleteFactoryInfoByIds(ids));
    }

    /**
     * 获取工厂管理员
     * @param id
     * @return
     */
    @GetMapping("/getFactoryManager/{id}")
    public AjaxResult getFactoryManager(@PathVariable("id") Long id){
        SysUser query = new SysUser();
        query.setBizId(id);
        query.setUserType(SysUser.USER_TYPE_1);
        List<SysUser> sysUsers = sysUserService.selectUserList(query);
        return AjaxResult.success(sysUsers);
    }

    @Log(title = "工厂管理员", businessType = BusinessType.INSERT)
    @PostMapping("/addManager")
    public AjaxResult addManager(@Validated @RequestBody SysUser user)
    {
        if (!sysUserService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !sysUserService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        SysRole factoryAdmin = sysRoleService.selectByKey("factoryAdmin");
        Long[] roles = new Long[]{factoryAdmin.getRoleId()};
        user.setRoleIds(roles);
        user.setUserType(SysUser.USER_TYPE_1);
        user.setCreateBy(getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setAvatar("https://smart-fuel.oss-cn-hangzhou.aliyuncs.com/2025/07/15/20250715110146.png");
        return toAjax(sysUserService.insertUser(user));
    }
}
